<template>
  <div class="ai-analysis-container">
    <div class="ai-analysis-header">
      <h1>AI情感解析</h1>
      <p>向AI倾诉你的情感问题，获得专业的分析和建议</p>
    </div>

    <div class="ai-analysis-content">
      <div class="chat-container">
        <div class="chat-messages" ref="chatContainer">
          <div 
            v-for="(message, index) in messages" 
            :key="index" 
            :class="['message', message.role]"
          >
            <div class="message-content">
              <div class="avatar">
                <span v-if="message.role === 'user'">👤</span>
                <span v-else>🤖</span>
              </div>
              <div class="text">
                <p>{{ message.content }}</p>
              </div>
            </div>
          </div>
        </div>

        <div class="chat-input">
          <textarea 
            v-model="userInput" 
            placeholder="请输入您的情感问题..." 
            @keyup.enter="sendMessage"
            :disabled="isLoading"
          ></textarea>
          <button @click="sendMessage" :disabled="isLoading || !userInput.trim()">
            {{ isLoading ? '分析中...' : '发送' }}
          </button>
        </div>
      </div>

      <div class="ai-info">
        <h2>AI情感解析说明</h2>
        <ul>
          <li>AI会根据您提供的情感问题给出专业分析</li>
          <li>请尽量详细描述您的情况，以便获得更准确的建议</li>
          <li>AI的建议仅供参考，不能替代专业心理咨询</li>
          <li>我们会保护您的隐私，不会存储您的对话内容</li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'AIAnalysisView',
  data() {
    return {
      userInput: '',
      messages: [
        {
          role: 'ai',
          content: '您好！我是情感解析AI助手。请告诉我您遇到的情感问题，我会尽力为您提供分析和建议。'
        }
      ],
      isLoading: false
    }
  },
  methods: {
    async sendMessage() {
      if (!this.userInput.trim() || this.isLoading) return

      // 添加用户消息
      this.messages.push({
        role: 'user',
        content: this.userInput
      })

      // 保存用户输入并清空输入框
      const question = this.userInput
      this.userInput = ''
      this.isLoading = true

      // 滚动到底部
      this.$nextTick(() => {
        this.scrollToBottom()
      })

      try {
        // 模拟AI回复（实际项目中这里会调用真实的AI API）
        await new Promise(resolve => setTimeout(resolve, 1000))
        
        // 生成模拟回复
        const aiResponse = this.generateAIResponse(question)
        
        // 添加AI回复
        this.messages.push({
          role: 'ai',
          content: aiResponse
        })
      } catch (error) {
        this.messages.push({
          role: 'ai',
          content: '抱歉，分析过程中出现了问题，请稍后重试。'
        })
      } finally {
        this.isLoading = false
        this.$nextTick(() => {
          this.scrollToBottom()
        })
      }
    },
    generateAIResponse(question) {
      // 这里是模拟的AI回复逻辑，实际项目中应该调用真实的AI API
      const responses = [
        `我理解您关于"${question.substring(0, 10)}..."的问题。情感问题往往需要时间和耐心来解决。建议您可以尝试与对方进行坦诚的沟通，表达您的真实感受，同时也要倾听对方的想法。`,
        `针对您提到的"${question.substring(0, 10)}..."情况，我认为关键在于理解彼此的需求和期望。建议您先冷静分析问题的根源，然后寻找双方都能接受的解决方案。`,
        `您遇到的"${question.substring(0, 10)}..."情况确实需要认真对待。感情中的问题没有标准答案，但真诚和尊重是解决问题的基础。建议您保持开放的心态，必要时也可以寻求专业的情感咨询师帮助。`,
        `关于"${question.substring(0, 10)}..."的问题，我想说每个人在感情中都会遇到挑战。重要的是要相信自己有能力处理这些问题。建议您先照顾好自己的情绪，再尝试解决关系中的矛盾。`
      ]
      
      return responses[Math.floor(Math.random() * responses.length)]
    },
    scrollToBottom() {
      const container = this.$refs.chatContainer
      if (container) {
        container.scrollTop = container.scrollHeight
      }
    }
  },
  mounted() {
    this.scrollToBottom()
  }
}
</script>

<style scoped>
.ai-analysis-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 2rem;
}

.ai-analysis-header {
  text-align: center;
  margin-bottom: 3rem;
}

.ai-analysis-header h1 {
  font-size: 2.5rem;
  color: #333;
  margin-bottom: 1rem;
}

.ai-analysis-header p {
  color: #666;
  font-size: 1.1rem;
}

.ai-analysis-content {
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: 2rem;
}

.chat-container {
  background-color: #fff;
  border-radius: 10px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  height: 600px;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.message {
  display: flex;
}

.message.user {
  justify-content: flex-end;
}

.message.ai {
  justify-content: flex-start;
}

.message-content {
  display: flex;
  gap: 0.8rem;
  max-width: 80%;
}

.user .message-content {
  flex-direction: row-reverse;
}

.avatar {
  font-size: 1.5rem;
}

.text {
  background-color: #f1f3f4;
  padding: 1rem;
  border-radius: 10px;
}

.user .text {
  background-color: #ff9a9e;
  color: white;
}

.ai .text {
  background-color: #e9ecef;
}

.text p {
  margin: 0;
  line-height: 1.5;
}

.chat-input {
  display: flex;
  padding: 1.5rem;
  border-top: 1px solid #eee;
  gap: 1rem;
}

.chat-input textarea {
  flex: 1;
  padding: 1rem;
  border: 1px solid #ddd;
  border-radius: 5px;
  resize: none;
  height: 80px;
  font-family: inherit;
}

.chat-input textarea:focus {
  outline: none;
  border-color: #ff9a9e;
}

.chat-input button {
  padding: 0 1.5rem;
  background-color: #ff9a9e;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  font-weight: bold;
}

.chat-input button:hover:not(:disabled) {
  background-color: #f86d75;
}

.chat-input button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.ai-info {
  background-color: #fff;
  border-radius: 10px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  padding: 1.5rem;
  align-self: flex-start;
}

.ai-info h2 {
  font-size: 1.3rem;
  margin-bottom: 1rem;
  color: #333;
}

.ai-info ul {
  list-style-type: none;
  padding: 0;
}

.ai-info li {
  padding: 0.5rem 0;
  border-bottom: 1px solid #eee;
  color: #666;
}

.ai-info li:last-child {
  border-bottom: none;
}

.ai-info li:before {
  content: "✓";
  color: #ff9a9e;
  font-weight: bold;
  display: inline-block;
  width: 1em;
  margin-right: 0.5em;
}

@media (max-width: 992px) {
  .ai-analysis-content {
    grid-template-columns: 1fr;
  }
  
  .ai-info {
    align-self: stretch;
  }
}

@media (max-width: 768px) {
  .ai-analysis-container {
    padding: 1rem;
  }
  
  .ai-analysis-header h1 {
    font-size: 2rem;
  }
  
  .chat-container {
    height: 500px;
  }
  
  .message-content {
    max-width: 90%;
  }
}
</style>